'''参考最后一个实例，尝试将不同标签中的内哦能够分门别类地题取出来，再想想如何提取可以更为准确。
（提示：可以参考HTML相关语法）'''
def askFileName():
    print('{:*^50}'.format('本程序用于抓取百度文库网页内的文本内容')+'\n')
    print('注意事项：请把在浏览器中Ctrl+S将网页保存，模式选择“整个网页”，\n    并将.HTML文件放到本程序同一目录下\n')
    fileName = input('请输入你要打开的文件名：')
    return fileName

def readFile(fileName:'str'):
    f = open(fileName,'r',encoding = 'utf-8')
    htmlList = f.readlines()
    f.close()
    print('文件已读取完成！')
    return htmlList

def getNewFileName(htmlList):
    for line in htmlList:
        if 'h3 class="doc-title"' in line:
            newFileName = line.split('<h3 class="doc-title">')[1].split('</h3')[0]   
    print('已获得新文件名为：'+newFileName)
    return newFileName

def getUrl(htmlList):
    url = ''
    for line in htmlList:
        if 'saved from url=' in line:
            url = 'https://' + line.split('https://')[1].split('.html')[0] + '.html'
    return url

def getTxt(htmlList):
    readed,counted,tallPage,page = 0,0,0,1
    aLine,txtListTemp,txtList,i = '',[],[],1
    for line in htmlList:   #此处需要针对优化长标签断行的情况进行优化9.4记
        if 'class="doc-summary-wrap' in line:
            tallPage = line.split('</em>页')[0].split('<em>')[-1]
    for line in htmlList:
        readed += 1
        cutLine = line.split('</p>')
        for item in cutLine:
            if 'class="reader-word-layer reader-word' in item:
                aLine = item.split('">')[-1]            
                txtListTemp.append(aLine)
                counted += 1
                print('目前正执行第{}页,共{}页，已读取源文件{}行，已记录{}行...'.format(page,tallPage,readed,counted))
            elif 'class="reader-txt-layer"' in line:
                i += 1
                aLine = '\n===================此处开始第'+str(i)+'页===================\n\n'
                txtListTemp.append(aLine)
                page += 1
                print('目前正执行第{}页,共{}页，已读取源文件{}行，已记录{}行...'.format(page,tallPage,readed,counted))
    for i in range(len(txtListTemp)):
        if txtListTemp[i] == '':
            txtList.append('\n')
        else:
            txtList.append(txtListTemp[i])
    
    print('{:=^50}'.format('已获取全部可用文本！'))
    return  txtList

def writeTxt(newFileName,txtList,url):
    line,txtListFinal = '',[]
    for i in range(len(txtList)):
        if '\n' in txtList[i] :
            line += txtList[i]
            txtListFinal.append(line)
            line = ''
        else:
            line += txtList[i]
    #print(txtListFinal)
    print('\n===================已创建新文件{}.txt===================\n'.format(newFileName))
    COPYRIGHT = '''
**************************************
@Author:TQ
@Version:0.0.1
@Date:2021.9.4
@原文地址：
'''
    comment = COPYRIGHT.strip('\n') + url +'\n**************************************\n\n'
    txtListFinal.insert(0,comment)
    f = open(newFileName+'.txt','w',encoding='utf-8')
    for item in txtListFinal:
        f.write(item)
    f.close()

def main():
    fileName = askFileName()
    htmlList = readFile(fileName)
    newFileName = getNewFileName(htmlList)
    url = getUrl(htmlList)
    txtList = getTxt(htmlList)
    writeTxt(newFileName,txtList,url)

main()
    
    
    

